//
//  Date+Extensions.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/21.
//

import UIKit
import MYModule
import SwiftDate

extension DateInRegion {
    
    public func localString(dateFormat: String) ->String {
        return self.date.localString(dateFormat: dateFormat)
    }
    
    public var localString: String {
        
        return self.date.localString
         
     }
     
     public var localStringOfMonthWeek: String {
         
         return self.date.localStringOfMonthWeek
         
     }
    
  
    
}

extension Date {
    
    public func localString(dateFormat: String) ->String {
        let formatter = DateFormatter.localDateFormatter(dateFormat: dateFormat)
        return formatter.string(from: self)
    }
    
    
    /// 年月日周显示
    public var localString: String {
        
        let formatter = DateFormatter.localDateFormatter
        
        formatter.dateFormat = "global_date_format".language
        return formatter.string(from: self)
   
    }
    
    public var localStringOfDateTime: String {
        
        let formatter = DateFormatter.localDateFormatter
        formatter.dateFormat = "home_item_date_format".language
        return formatter.string(from: self)
        
    }
    
    
    /// 当天 月+ 周显示
    public var localStringOfMonthWeek: String {
        
    
        let formatter = DateFormatter.localDateFormatter

        formatter.dateFormat = "today_date_format".language
        return formatter.string(from: self)
        
    }
    
    /// 本月日期显示
    public var localMonthRangeString: String {
        let startDate = Date(year: self.year, month: self.month, day: 1, hour: 0, minute: 0)
        let endDate = Date(year: self.year, month: self.month, day: self.monthDays, hour: 0, minute: 0)
        return Date.localRangeString(start: startDate, end: endDate)
        
    }
    
    /// 最近一周日期显示
    public var localweekRangeString: String {
        let startDate = date - (date.weekday - 1).days
        let endDate = startDate + 6.days
        return Date.localRangeString(start: startDate, end: endDate)
        
    }
    
    /// 最近7天日期显示
    public var localSevenDayRangeString: String {
        let startDate = date - 6.days
        return Date.localRangeString(start: startDate, end: date)
    }
    
    public static func localRangeString(start date: Date, end date1: Date, format: String = "%02d") -> String {
        
        let startDateString = date.localString
        let endDateString = date1.localString
        
        return "\(startDateString) - \(endDateString)"
        
    }
    
    public var dateStringOfYearMonthDayHourMinute: String {
        
        let formatter = DateFormatter.localDateFormatter
        formatter.dateFormat = "home_workout_records_item_date_format".language
        return formatter.string(from: self)
        
    }

}

public extension DateFormatter {
    
    
    static func localDateFormatter(dateFormat: String) -> DateFormatter {
        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone.localTimeZone
        dateFormatter.dateFormat = dateFormat
        return dateFormatter
    }
    
    static var localDateFormatter: DateFormatter {
        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone.localTimeZone
        return dateFormatter
    }
    
}

public extension TimeZone {
    
   static var localTimeZone: TimeZone {
        
        return TimeZone.current
    }
    
}
